﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Clarendon.AzureBilling.Library
{
    /// <summary>
    /// Parses a service response 
    /// </summary>
    public class XmlParseDeployments : IParseResponseXmlServices
    {
        public T ParseXmlString<T>(string xml)
        {
            XDocument document = XDocument.Parse(xml, LoadOptions.None);

            Trace.WriteLine("Parsing XML response and interpreting <Deployment/> nodes");
            var deployments = (from item in document.Descendants(QueryManager.BuildDefaultNamespaceXmlEntity("Deployment"))
                               select new Deployment()
                               {
                                   Name = (string)item.Element(QueryManager.BuildDefaultNamespaceXmlEntity("Name")),
                                   Url = (string)item.Element(QueryManager.BuildDefaultNamespaceXmlEntity("Url")),
                                   Status = (string)item.Element(QueryManager.BuildDefaultNamespaceXmlEntity("Status")),
                                   RoleInstanceList = (from instance in item.Descendants("RoleInstance")
                                                      select new RoleInstance()
                                                                 {
                                                                     RoleName = (string)instance.Element((QueryManager.BuildDefaultNamespaceXmlEntity("RoleName"))),
                                                                     InstanceName = (string)instance.Element((QueryManager.BuildDefaultNamespaceXmlEntity("InstanceName"))),
                                                                     InstanceStatus = (string)instance.Element((QueryManager.BuildDefaultNamespaceXmlEntity("InstanceStatus"))),
                                                                     VmSize = (string)instance.Element((QueryManager.BuildDefaultNamespaceXmlEntity("InstanceSize")))

                                                                 }).ToList()
                               }).ToList();
            Trace.WriteLine("Finished parsing XML response and interpreted <Deployment/> nodes");
          
            return (T)(object)deployments;
        }
    }
}
